from app.models.user_model import User
from app.database import get_db

def create_user(username: str, email: str, password: str):
    """插入用户"""
    db = get_db()
    try:
        with db.atomic():
            user = User.create(username=username, email=email, password=password)
            return user
    except Exception as e:
        print(f"创建用户失败: {e}")
        return None

def get_user_by_id(user_id: int):
    """通过用户ID获取用户"""
    try:
        return User.get(User.id == user_id)
    except User.DoesNotExist:
        return None

def get_user_by_username(username: str):
    """通过用户名获取用户"""
    try:
        return User.get(User.username == username)
    except User.DoesNotExist:
        return None

def get_user_by_email(email: str):
    """通过邮箱获取用户"""
    try:
        return User.get(User.email == email)
    except User.DoesNotExist:
        return None

#获取全部用户
def get_users():
    """获取全部用户"""
    try:
        return User.select()
    except Exception as e:
        print(f"获取用户失败: {e}")
        return None